package com.gopay.bis.service.bank.impl.ebank;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.common.util.BisPayCheckUtils;
import com.gopay.bis.common.util.constants.BisPayConstants;
import com.gopay.bis.manager.bank.ebank.GdbB2cManager;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.constants.bank.GdbConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.gdb.ebank.GdbB2cMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BankInteractRemoteService;

/**
 * 广发B2C网关支付
 * 
 * @GdbB2cBankServiceImpl.java
 * @author zhengxue
 * @2014-11-12 下午3:11:20 www.gopay.com.cn Inc.All rights reserved.
 */
public class GdbB2cBankServiceImpl implements BankInteractRemoteService {
    @Resource(name = "gdbB2cManager")
    private GdbB2cManager gdbB2cManager;

    /**
     * 支付
     */
    public Result pay(BaseMessage base) {
        Result result = new Result(false);
        try {
            GdbB2cMessage messageB2C = (GdbB2cMessage) gdbB2cManager.sign(base);
            result.setDefaultModel(messageB2C);
            result.setSuccess(true);
        } catch (GopayException e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            result.setResultCode(e.getErrCode());
        } catch (Exception ex) {
            BankLogUtils.printServiceImplExceptionLog(ex);
            result.setResultCode(ProcCodeConstants.PROC_CODE_100E6029);
        }
        return result;
    }

    /**
     * 返回结果验签
     */
    public Result bankBack(BaseMessage base) {
        Result result = new Result(false);
        try {
            GdbB2cMessage messageB2C = new GdbB2cMessage();
            BeanUtils.copyProperties(base, messageB2C);
            boolean flag = (Boolean) gdbB2cManager.verify(messageB2C);
            result.setModel("message", messageB2C);
            result.setDefaultModel(flag);
            result.setSuccess(true);
            if (flag) {
            	 
            	 if (messageB2C.getBankBusProd().equals(BisBankBusinessProd._11)) {
            		 //如果为wap网银支付
            		 // 验签成功
                     Map<String, String> param = new HashMap<String, String>();
                     if (GdbConstants.WAP_PAY_STATUS_SUCCESS.equals(messageB2C.getPayState())) {
                         // 支付成功入账
                         param.put("orderId", messageB2C.getOrderId());
                         param.put("amt", messageB2C.getAmount().toString());
                         param.put("cardType", "Unknown");
                         param.put("notifyNum", "1");
                         param.put("payStatus", "00");
                         param.put("retCode", "00");
                         param.put("retMsg", "验签成功");
                         param.put("signData", messageB2C.getSignMessage());
                         param.put("isFront", base.isFront() + "");
                         result.setModel("isNotifyCashier", param);
                         result.setOrderStatus(true);
                     } else {
                         // 通知后台地址该订单失败
                         param.put("orderId", messageB2C.getOrderId());
                         param.put("amt", messageB2C.getAmount().toString());
                         param.put("cardType", "Unknown");
                         param.put("notifyNum", "1");
                         param.put("payStatus", "01");
                         param.put("retCode", "01");
                         param.put("retMsg", "验签失败");
                         param.put("signData", messageB2C.getSignMessage());
                         param.put("isFront", base.isFront() + "");
                         result.setModel("isNotifyCashier", param);
                         result.setOrderStatus(false);
                     }
            	 }
            	 else{
            		 // 验签成功
                     Map<String, String> param = new HashMap<String, String>();
                     if (GdbConstants.PAY_STATUS_SUCCESS.equals(messageB2C.getRetCode())) {
                         // 支付成功入账
                         param.put("orderId", messageB2C.getOrderId());
                         param.put("amt", messageB2C.getAmount().toString());
                         param.put("cardType", "Unknown");
                         param.put("notifyNum", "1");
                         param.put("payStatus", "00");
                         param.put("retCode", "00");
                         param.put("retMsg", "验签成功");
                         param.put("signData", messageB2C.getSignMessage());
                         param.put("isFront", base.isFront() + "");
                         result.setModel("isNotifyCashier", param);
                         result.setOrderStatus(true);
                     } else {
                         // 通知后台地址该订单失败
                         param.put("orderId", messageB2C.getOrderId());
                         param.put("amt", messageB2C.getAmount().toString());
                         param.put("cardType", "Unknown");
                         param.put("notifyNum", "1");
                         param.put("payStatus", "01");
                         param.put("retCode", "01");
                         param.put("retMsg", "验签失败");
                         param.put("signData", messageB2C.getSignMessage());
                         param.put("isFront", base.isFront() + "");
                         result.setModel("isNotifyCashier", param);
                         result.setOrderStatus(false);
                     }
            	 }
               
            }
        } catch (GopayException e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            result.setResultCode(e.getErrCode());
        } catch (Exception ex) {
            BankLogUtils.printServiceImplExceptionLog(ex);
            result.setResultCode(ProcCodeConstants.PROC_CODE_100E6030);
        }

        return result;
    }

    /**
     * 订单查询
     */
    public Result query(BaseMessage base) {
        Result result = new Result(false);
        try {
            GdbB2cMessage messageB2C = new GdbB2cMessage();
            BeanUtils.copyProperties(base, messageB2C);
            GdbB2cMessage queryB2C = (GdbB2cMessage) gdbB2cManager.query(messageB2C);
            result.setDefaultModel(queryB2C);
            result.setSuccess(true);
            if (BisPayCheckUtils.checkExternalOrderID(messageB2C.getOrderId())) {
                Map<String, String> param = new HashMap<String, String>();
                param.put("orderId", messageB2C.getOrderId());
                param.put("amount", messageB2C.getAmount().toString());
                if (queryB2C == null) {
                    // 查询订单为空
                    param.put("returnCode", BisPayConstants.QUERYSTATE_FAIL);
                    param.put("returnMessage", "银行返回对账结果为空");
                    param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
                    result.setModel("queryResult", param);
                    return result;
                }

                if (!(GdbConstants.QUERY_STATUS_SUCCESS.equals(queryB2C.getOrderStatus()))) {
                    // 查询订单失败
                    param.put("returnCode", BisPayConstants.QUERYSTATE_FAIL);
                    param.put("returnMessage", "银行订单查询失败" + queryB2C.getRetCode());
                    param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
                } else {
                    // 查询成功
                    // 成功处理
                    if (GdbConstants.PAY_STATUS_SUCCESS.equalsIgnoreCase(queryB2C.getRetCode())) {
                        param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
                        param.put("tradeState", BisPayConstants.TRADESTATE_SUCCESS);
                        param.put("returnMessage", "交易成功");
                    }
                    // 失败处理
                    else if (!(GdbConstants.PAY_STATUS_SUCCESS.equalsIgnoreCase(queryB2C.getRetCode()))) {
                        param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
                        param.put("tradeState", BisPayConstants.TRADESTATE_FAIL);
                        param.put("returnMessage", "订单处理失败");
                    }
                }
                result.setModel("queryResult", param);
            }
        } catch (GopayException e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            result.setResultCode(e.getErrCode());
        } catch (Exception ex) {
            BankLogUtils.printServiceImplExceptionLog(ex);
            result.setResultCode(ProcCodeConstants.PROC_CODE_100E6033);
        }
        return result;
    }

    @Override
    public Result dayend(BaseMessage arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Result queryBack(BaseMessage arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Result refund(BaseMessage arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Result refundBack(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Result refundQuery(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

	@Override
	public Result sendBankRefund(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

}
